<?php
session_start();
include('menu.php');
include('ajout_maison.html');

if( !empty($_POST["location_voie"])){
	$location_voie = $_POST['location_voie'];
	$location_desc = $_POST['location_desc'];
	$location_surface = $_POST['location_surface'];
	$location_couv = $_POST['location_couv'];
	$location_tarifh = $_POST['location_tarifh'];
	$location_tarifj = $_POST['location_tarifj']; 
	$location_ville = $_POST['location_ville'];
	$location_pays = $_POST['location_pays'];
	$location_type = $_POST['location_type'];
	$location_date_debut = $_POST["location_date_debut"];
	$location_date_fin = $_POST["location_date_fin"];
	$user_id=$_SESSION["user_id"];



	$date_debut=date('Y-m-d', strtotime($location_date_debut));
	$date_fin=date('Y-m-d', strtotime($location_date_fin));

	echo $date_debut;
	echo $date_fin;





	
	$query = "INSERT IGNORE Pays VALUES('', '$location_pays')";
	$res=mysqli_query($connexion,$query);
	

	$query = "Select * From pays where nom_pays = '$location_pays'";
	$res = mysqli_query($connexion, $query);
	$row = mysqli_fetch_array($res);
	$id_pays = $row['pays_id'];



	$query = "INSERT IGNORE Ville VALUES('','$location_ville', $id_pays)";
	$res=mysqli_query($connexion,$query);


	$query = "Select * From ville where ville_nom = '$location_ville' and pays_id=$id_pays";
	echo $query;
	echo "<br/>";
	$res = mysqli_query($connexion, $query);
	$row = mysqli_fetch_array($res);
	$id_ville = $row['ville_id'];


	
	$query = "UPDATE `fbdb`.`user` SET `utype_id` = 5 WHERE `user`.`user_id` = $user_id ";
	$res = mysqli_query($connexion, $query);
	$query = "INSERT INTO `fbdb`.`location` (`location_id`, `location_voie`, `location_desc`, `location_etat`, `location_surface`, `location_couv`, `location_dated`, `location_datef`, `location_tarifh`, `location_tarifj`, `user_id`, `ville_id`, `locatype_id`) VALUES (NULL, '$location_voie', '$location_desc', '0', '$location_surface', '$location_couv', '$date_debut', '$date_fin', '$location_tarifh', '$location_tarifj', '$user_id', '$id_ville', '$location_type');";
	echo $query;
	$res = mysqli_query($connexion, $query);

	header("location:ajout_maison_ok.php");

}

?>